import Axios from 'axios'
import sign from './sign.js'

// 新建一个 axios 实例
const resquest = Axios.create({
    baseURL: process.env.NODE_ENV === 'development' ? '/' : window.HTTP_URL
})
// 添加请求拦截器
resquest.interceptors.request.use(
    config => {
        var reqData = sign.md5_sign(config.data)
        var formData = new FormData()
        Object.entries(reqData).forEach(([key, value]) => {
            formData.append(key, value)
        })
        config.data = formData
        return config
    },
    err => {
        console.log(err)
        return Promise.reject(err)
    }
)
// 添加响应拦截器
resquest.interceptors.response.use(
    res => {
        const { data = {} } = res
        if (data.code == '200') {
            return data
        } else {
            return Promise.reject(new Error(data))
        }
    },
    err => {
        console.log('err' + err)
        return Promise.reject(err)
    }
)


var httpRequest = {
    post: function(url, params) {
        return resquest.post(url, params)
    },

    get: function(url, params) {
        return resquest.get(url, params)
    },
}

export default httpRequest
